/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @update 2011.11.19
 * @prompt If you find my mistake, please contact with me via my e-mail.
 */
package API_sample
{
	import flash.display.Sprite;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	public class Utils_sample extends Sprite
	{
		private var delay1:Number = 1000;
		private var delay2:Number = 1500;
		private var setIntervalID1:uint;
		private var setIntervalID2:uint;
		private var start1:uint = 0;
		private var start2:uint = 0;
		private var stop:uint = 5;
		
		private var delay:Number = 10;
		private var setTimeoutID:uint;
		private var count:uint = 1;
		private var i:uint = 0;
		
		public function Utils_sample()
		{
			//setInterval_example();
			setTimeout_example();
		}
		
		/**
		 * setInterval(closure:Function, delay:Number, ... arguments):uint
		 * ... arguments是传递给 closure 函数的可选参数列表
		 * 以指定的间隔（以毫秒为单位）运行函数
		 */		
		private function setInterval_example():void
		{
			setIntervalID1 = setInterval(setIntervalFunction1,delay1);
			setIntervalID2 = setInterval(setIntervalFunction2,delay2);
		}
		
		private function setIntervalFunction1():void
		{
			start1++;
			trace("setIntervalFunction1_repeat"+start1+"_time"+getTimer());
			if(start1==stop)
				clearInterval(setIntervalID1);
		}
		
		private function setIntervalFunction2():void
		{
			start2++;
			trace("setIntervalFunction2_repeat"+start2+"_time"+getTimer());
			if(start2==stop)
				clearInterval(setIntervalID2);
		}
		
		/**
		 * setTimeout(closure:Function, delay:Number, ... arguments):uint 
		 * ... arguments是传递给 closure 函数的可选参数列表
		 * 在指定的延迟（以毫秒为单位）后运行指定的函数
		 */		
		private function setTimeout_example():void
		{
			setTimeoutID = setTimeout(setTimeoutFunction,delay);
			startCounting();
		}
		
		private function setTimeoutFunction():void
		{
			trace("在" + delay/1000 +"秒时计数" + i + "次时");
			trace("setTimeoutFunction_time"+getTimer());
			clearTimeout(setTimeoutID);
		}
		
		private function startCounting():void
		{
			while(i < count)
			{
				i++;
				if(i == count)
				{
					trace(i);
				}
			}
		}
	}
}